import { get, makeObservable, observable, computed, runInAction, action } from 'mobx'
import AppleItemStore from './AppleItemStore'


class AppleBasketStore {
  currentApples = [];
  eatedApples = []
  isPicking = false

  constructor (currentApples) {
    if (currentApples) {
      this.currentApples = currentApples
    }
    makeObservable(this, {
      currentApples: observable,
      eatedApples: observable,
      isPicking: observable,
      countAppleWeight: computed,
      countEatedApple: computed,
      getApples: action.bound
    }) 
  }

  get countAppleWeight () {
    return this.currentApples.reduce((total, apple) => total + apple.weight, 0)
  }

  get countEatedApple () {
    return this.eatedApples.reduce((total, apple) => total + apple.weight, 0)
  }

  getApples () {
    this.isPicking = true
    setTimeout(() => {
      runInAction(() => {
        this.currentApples.push(new AppleItemStore(this.currentApples.length))
        this.isPicking = false
      })
    }, 1000)
    
  }
}

export default AppleBasketStore